$(function() {
    // 用户统计
    // 每日登录量
    var loginChart = echarts.init(document.getElementById("echarts-login"));
    axios.get("http://127.0.0.1:8000/statistics/?field=login").then(
        function(response) {
            var resp = response.data
            var date = resp['title_list']
            var data = resp['data_list']
            console.log(resp)
            loginChart.setOption({
                title: {
                    text: '每日用户登录数'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['用户数']
                },
                grid: {
                    x: 40,
                    x2: 40,
                    y2: 24
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    data: date
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: '{value}'
                    }
                }],
                series: [{
                    name: '用户数',
                    type: 'line',
                    data: data,
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                            { type: 'min', name: '最小值' }
                        ]
                    },
                    markLine: {
                        data: [
                            { type: 'average', name: '平均值' }
                        ]
                    }
                }, ]
            });

            $(window).resize(loginChart.resize);
        },
        function(err) {
            console.log(err)
            $(window).resize(loginChart.resize);
        }
    )

    // 每日新增用户数
    var useraddChart = echarts.init(document.getElementById("echarts-useradd"));
    axios.get("http://127.0.0.1:8000/statistics/?field=useradd").then(
        function(response) {
            var resp = response.data
            var date = resp['title_list']
            var data = resp['data_list']
            console.log(resp)
            useraddChart.setOption({
                title: {
                    text: '每日新增用户数'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['用户数']
                },
                grid: {
                    x: 40,
                    x2: 40,
                    y2: 24
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    data: date
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: '{value}'
                    }
                }],
                series: [{
                    name: '用户数',
                    type: 'line',
                    data: data,
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                            { type: 'min', name: '最小值' }
                        ]
                    },
                    markLine: {
                        data: [
                            { type: 'average', name: '平均值' }
                        ]
                    }
                }, ]
            });

            $(window).resize(useraddChart.resize);
        },
        function(err) {
            console.log(err)
            $(window).resize(useraddChart.resize);
        }
    )

    // 累计用户总数
    var userallChart = echarts.init(document.getElementById("echarts-userall"));
    axios.get("http://127.0.0.1:8000/statistics/?field=useradd&type=add").then(
        function(response) {
            var resp = response.data
            var date = resp['title_list']
            var data = resp['data_list']
            console.log(resp)
            userallChart.setOption({
                title: {
                    text: '累计用户总数'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['用户数']
                },
                grid: {
                    x: 40,
                    x2: 40,
                    y2: 24
                },
                calculable: true,
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    data: date
                }],
                yAxis: [{
                    type: 'value',
                    axisLabel: {
                        formatter: '{value}'
                    }
                }],
                series: [{
                    name: '用户数',
                    type: 'bar',
                    data: data,
                    markPoint: {
                        data: [
                            { type: 'max', name: '最大值' },
                            { type: 'min', name: '最小值' }
                        ]
                    },
                    markLine: {
                        data: [
                            { type: 'average', name: '平均值' }
                        ]
                    }
                }, ]
            });

            $(window).resize(userallChart.resize);
        },
        function(err) {
            console.log(err)
            $(window).resize(userallChart.resize);
        }
    )

})